/* Given a set of symbols, our task is to find a longest valid expression
 * using these symbols. The symbols can be divided into three classes:
 *
 *   - primitives: p, q, r, s, t
 *   - unary operator: N
 *   - binary operators: A, C, E, K
 *
 * Clearly we must have primitive symbols to build an expression. Secondly,
 * we can put all 'N's before a primitive. Then we link every two sub-
 * expressions using any binary operator to form a new sub-expression. We
 * continue this until either the sub-expressions or the binary operators
 * are exhausted.
 *
 * Tractically, we can easily rearrange the input symbols into three parts
 * using basically the same technique as counting sort:
 * 
 *   [ACEK]* [N]* [pqrst]*
 *
 * Let the length of these three parts be L1, L2 and L3 respectively. We then
 * truncate the expression so that L1 = L3 - 1.
 */

#include <stdio.h>
#include <string.h>

static int get_longest_expr(const char *s, char expr[])
{
    int L1 = 0, L2 = 0, L3 = 0, L;
    int i, i1, i2, i3;
    for (i = 0; s[i]; i++)
    {
        if (s[i] == 'N')
            ++L2;
        else if (s[i] >= 'p')
            ++L3;
        else 
            ++L1;
    }

    if (L3 == 0)
        return 0;
    if (L1 > L3 - 1)
        L1 = L3 - 1;
    else
        L3 = L1 + 1;

    i1 = 0;
    i2 = L1;
    i3 = L1 + L2;
    L = L1 + L2 + L3;
    for (i = 0; s[i]; i++)
    {
        if (s[i] == 'N')
            expr[i2++] = 'N';
        else if (s[i] >= 'p')
        {
            if (i3 < L)
                expr[i3++] = s[i];
        }
        else
        {
            if (i1 < L1)
                expr[i1++] = s[i];
        }
    }
    expr[L] = '\0';
    return 1;
}

int main()
{
    char line[120], expr[120];
    while (scanf("%s", line) == 1 && line[0] != '0')
    {
        if (get_longest_expr(line, expr))
            printf("%s\n", expr);
        else
            printf("no WFF possible\n");
    }
    return 0;
}
